/**
 * @author: lichuang.
 * @date: 2019/2/16
 * @description:
 */

import CryptoJS from "crypto-js"
import moment from "./moment.min"

let Encrypt = Encrypt || {}

//base64加密
Encrypt.Base64 = (data) => {
  let wordArray = CryptoJS.enc.Utf8.parse(data)
  let base64 = CryptoJS.enc.Base64.stringify(wordArray)
  return base64
}

//AES解密
Encrypt.decryptByAES = (ciphertext) => {
  let keyHex = CryptoJS.enc.Utf8.parse('hnwxhnwxhnwxhnwx')
  let decrypted = CryptoJS.AES.decrypt({
    ciphertext: CryptoJS.enc.Base64.parse(ciphertext.replace(/\s|\r|\n/g, ''))
  }, keyHex, {
    iv: keyHex,
    mode: CryptoJS.mode.CBC
  })
  return decrypted.toString(CryptoJS.enc.Utf8)
}

//DES加密
Encrypt.encryptByDES = () => {
  let keyHex = CryptoJS.enc.Utf8.parse('bluestar')
  let encrypted = CryptoJS.DES.encrypt(moment().format('YYYY-MM-DD hh:mm:ss'), keyHex, {
    iv: keyHex,
    mode: CryptoJS.mode.CTR,
    //padding: CryptoJS.pad.Pkcs7
  })
  return encrypted.toString()
}

export default Encrypt
